﻿using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.Design;
using Windows.Basic.Infrastructure.EFCore;
using Windows.Infrastructure.Share.Extensions;
using Windows.Infrastructure.EFCore.Share.Extensions;
using Windows.Service.Share;
using Windows.Basic.Domain;
using Windows.Infrastructure.Share.CustomException;

namespace Windows.Basic.Service
{
    public class DictionaryService : IDictionaryService
    {
        private readonly BasicDbContext _db;
        private readonly IMapper _mapper;
        public DictionaryService(BasicDbContext db, IMapper mapper)
        {
            _db = db;
            _mapper = mapper;
        }
        /// <summary>
        /// 字典列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PageResponse<DictionaryCategoryResponse>> Query(PageRequest<DictionaryCategoryRequest> request)
        {
            var response = new PageResponse<DictionaryCategoryResponse>();
            IQueryable<DictionaryCategory> query = _db.DictionaryCategory.Include(x => x.Dictionaries);
            if (!request.Query.Value.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.Value.Contains(request.Query.Value!));
            }
            if (!request.Query.Name.IsNullOrWhiteSpace())
            {
                query = query.Where(x => x.Name.Contains(request.Query.Name!));
            }
            var list = await query.Paging(request.PageIndex, request.PageSize).ToListAsync();
            response.TotalCount = await query.CountAsync();
            response.PageIndex = request.PageIndex;
            response.PageSize = request.PageSize;
            response.List = _mapper.Map<List<DictionaryCategoryResponse>>(list);
            return response;
        }
        /// <summary>
        /// 获取id的字典详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<DictionaryResponse> Detail(int id)
        {
            var model = await _db.Dictionary.FindByIdAsync(id);
            return _mapper.Map<DictionaryResponse>(model);
        }
        /// <summary>
        /// 字典保存
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <exception cref="ApiException"></exception>
        public async Task Save(DictionarySaveRequest request)
        {
            if (await _db.Dictionary.AnyAsync(x => x.Value == request.Value && x.Id != request.Id && x.DictionaryCategoryId == request.DictionaryCategoryId))
                throw new ApiException("字典值已存在!");
            if (await _db.Dictionary.AnyAsync(x => x.Name == request.Name && x.Id != request.Id && x.DictionaryCategoryId == request.DictionaryCategoryId))
                throw new ApiException("字典名称已存在!");
            if (request.Id == null)
            {
                Dictionary model = _mapper.Map<Dictionary>(request);
                await _db.Dictionary.AddAsync(model);
            }
            else
            {
                Dictionary? model = await _db.Dictionary.FindByIdAsync(request.Id.Value);
                if (model == null)
                    throw new ApiException("字典不存在!");
                model.Name = request.Name;
                model.Value = request.Value;
                model.Sort = request.Sort;
                model.Remark = request.Remark;
            }
            await _db.SaveChangesAsync();
        }
        /// <summary>
        /// 获取组合列表(可用于下拉框,复选框,单选框)
        /// </summary>
        /// <returns></returns>
        public async Task<List<DictionaryComboBoxResponse>> GetComboBoxs()
        {
            var list = await _db.DictionaryCategory.Include(x => x.Dictionaries).ToListAsync();
            return _mapper.Map<List<DictionaryComboBoxResponse>>(list);

        }
    }
}
